package hk.com.fwd.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import hk.com.fwd.biz.assembler.OriginalAssembler;
import hk.com.fwd.biz.exception.DmsException;
import hk.com.fwd.biz.util.TransformFiledUtil;
import hk.com.fwd.dao.*;
import hk.com.fwd.entity.Original;
import hk.com.fwd.model.PageBean;
import hk.com.fwd.model.OriginalDTO;
import hk.com.fwd.model.SubPageHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Service
@Transactional
public class OriginalBo extends BaseBo {

   @Autowired
   private OriginalDao originalDao;

   private static Logger logger = LogManager.getLogger(OriginalBo.class);



    private final static  String [] ARRAYFILED={"id","hui_user_name","hui_password"};



    public PageInfo searchOriginalByHelper(PageBean<OriginalDTO> pageBean)throws DmsException {
        logger.info("Start searchOriginal");

        OriginalDTO objDTO=pageBean.getPageData();
        Original pjo= OriginalAssembler.toEntity(objDTO);

        Page  page = SubPageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize(), TransformFiledUtil.transformDbModelFiled(pageBean.getSortStr(),ARRAYFILED));
        List<Original>  list=originalDao.getOriginalList(pjo);

        PageInfo info = new PageInfo<>(page.getResult());
        info.setList(OriginalAssembler.toDTOs(list));
        return info;

    }


    public int saveOriginal(OriginalDTO obj)throws DmsException{
        if(obj.getActionState().equals("Add")){
            return originalDao.createOriginal(OriginalAssembler.toEntity(obj));
        }else if(obj.getActionState().equals("Update")){
            return originalDao.modifyOriginal(OriginalAssembler.toEntity(obj));
        }
        return 0;
    }

    public int deleteOriginal(List<OriginalDTO> list)throws DmsException{
        int count=0;
        for(OriginalDTO it:list){
            count=count+originalDao.deleteOriginal(OriginalAssembler.toEntity(it));
        }
        return count;
    }





}
